<template>
  <div class="calendar">
    <!--  -->
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="small" style="width: 120px;margin-left:10px" @change="dateChange(1)">
        <el-option v-for="(item,index) in yearList" :key="index" :value="item" :label="item">
          {{ item }}
        </el-option>
      </el-select>
      <el-select v-model="currentMonth" size="small" style="width: 120px;margin-left:10px" @change="dateChange(1)">
        <el-option v-for="item in 12" :key="item" :value="item" :label="item">
          {{ item }}
        </el-option>
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeek(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  name: 'Calendar',
  filters: {
    getDay(value) {
      // console.log(value)
      // const nowDate = new Date(value)
      return new Date(value).getDate()
    }
  },
  props: {
    startDate: {
      type: Date,
      default: () => new Date()
    }
  },
  data() {
    return {
      currentMonth: null, // 当前月份
      currentYear: null, // 当前年份
      currentDate: null,
      yearList: []
    }
  },
  created() {
    this.currentMonth = this.startDate.getMonth() + 1
    this.currentYear = this.startDate.getFullYear()
    this.yearList = Array.from(Array(11), (value, index) => index - 5 + this.currentYear)
    this.dateChange(this.startDate.getDate())
  },
  methods: {
    dateChange(date = 1) {
      const year = this.currentYear
      const month = this.currentMonth
      this.currentDate = new Date(`${year}-${month}-${date}`)
    },
    isWeek(date) {
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style scoped>
.calendar /deep/ .el-calendar-day {
  height:  auto;
 }
.calendar /deep/ .el-calendar-table__row td,
.calendar /deep/ .el-calendar-table tr td:first-child,
.calendar /deep/ .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
.calendar /deep/ .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
.calendar /deep/ .el-calendar__header {
   display: none
 }
</style>
